package com.slz3.business.module.messageManagement.mesActivity.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.messageManagement.mesActivity.domain.MesActivity;
import com.slz3.business.module.messageManagement.mesActivity.domain.MesActivityFactory;
import com.slz3.business.module.messageManagement.mesActivity.steward.MesActivitySpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class MesActivityServiceImpl implements MesActivityService {
    @Override
    public MesActivity save(MesActivity mesActivity) {
        return Repositorys.mesActivity.save(MesActivityFactory.initBaseFields(mesActivity));
    }
    @Override
    public Page<MesActivity> page(MultiValueMap<String, String> params) {
        Page<MesActivity> mesActivityPage = Repositorys.mesActivity.findAll(
                MesActivitySpecification.findJoin(params), PageableSteward.extract(params)
        );
        return mesActivityPage;
    }


    @Override
    public MesActivity modify(MesActivity mesActivityNew) {
        return Repositorys.mesActivity.saveAndFlush(MesActivityFactory.initBaseFields(mesActivityNew));
    }

    @Override
    public Optional<MesActivity> single(UUID uuid) {
        return Repositorys.mesActivity.findById(uuid);
    }

    @Override
    public List<MesActivity> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.mesActivity.findAllById(uuids);
    }

    @Override
    public List<MesActivity> delete(List<MesActivity> mesActivityList) {
        mesActivityList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MesActivityFactory.initBaseFields(item);
        });
        return Services.mesActivityTransactional.batchModify(mesActivityList);
    }

}
